package com.sinsz.common.bolt;

import com.alipay.sofa.rpc.core.request.SofaRequest;
import com.alipay.sofa.rpc.filter.FilterInvoker;
import com.alipay.sofa.rpc.hystrix.DefaultSetterFactory;
import com.alipay.sofa.rpc.hystrix.SetterFactory;
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandProperties;

/**
 * Bolt设置工厂
 * @author chenjianbo
 */
public class BoltSetterFactory implements SetterFactory {

    private int timeout;

    public BoltSetterFactory(int timeout) {
        this.timeout = timeout;
    }

    public int getTimeout() {
        return timeout;
    }

    public void setTimeout(int timeout) {
        this.timeout = timeout;
    }

    @Override
    public HystrixCommand.Setter createSetter(FilterInvoker invoker, SofaRequest request) {

        DefaultSetterFactory factory = new DefaultSetterFactory();

        HystrixCommandProperties.Setter hcps = HystrixCommandProperties.Setter();

        hcps.withExecutionTimeoutEnabled(true);

        hcps.withExecutionTimeoutInMilliseconds(timeout);

        return factory.createSetter(invoker, request).andCommandPropertiesDefaults(hcps);
    }

}
